var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var userUrl = require("url/user");
var qqHttp = require("helper/http");
var listdata = mm("do_ListData");
var helperOpen = require("helper/open");
var noData = [ {
	"template" : 1
} ];
// 本页面-ajax业务请求url
var httpUrl = userUrl.url.suggest.managePage;

// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
var listView = ui("listView");
// 定义变量
var page = 1;

// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;

headerLeft.on("touch", function() {
	do_App.closePage()
});
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "建议反馈",
	"labelNext" : "反馈问题"
})
headerRight.on("touch", function() {
	helperOpen.open("source://view/user/suggest/add/main.ui");
})
header.refreshData();
// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	loadPage();
});
listView.bindItems(listdata);
// 刷新数据
// 刷新数据
function loadPage() {
	page = 1;
	var params = {
		page : page
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.removeAll();
		if (data.page.content.length>0) {
			listdata.addData(data.page.content);
		} else {
			listdata.addData(noData);
		}
		listView.refreshItems();
	})
}
// 刷新数据
function getNextPageData() {
	page++;
	var params = {
		page : page
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.addData(data.page.content);
		listView.refreshItems();
	})
}

// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		loadPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});
do_Page.on("result", function(data) {
	// 更新远程最新数据
	if(data&&data!=null&&"user_suggest_add"==data.fromPage){
		loadPage();
	}
});
// 点击一条数据
listView.on("touch", function(data) {
	var ondata = listdata.getOne(data);
	helperOpen.open("source://view/user/suggest/detail/main.ui", {
		id : ondata.id
	});
});
do_Page.on("deleteSuggest", function(data) {
	var alldata = listdata.getRange(0);
	for(var i = 0;i<alldata.length;i++){
		if(alldata[i].id==data){
			listdata.removeData([i]);
			listView.refreshItems();
			break;
		}
	}
	if(alldata.length==1){
		listdata.addData(noData);
		listView.refreshItems();
	}
})
